package com.zy.pro.shop.web;

import java.math.BigDecimal;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zy.core.model.PageView;
import com.zy.core.web.BaseController;
import com.zy.pro.city.service.CityService;
import com.zy.pro.security.model.Security;
import com.zy.pro.security.model.ShopSecurity;
import com.zy.pro.security.service.SecurityService;
import com.zy.pro.security.service.ShopSecurityService;
import com.zy.pro.security.web.form.SecuritySearchForm;
import com.zy.pro.security.web.form.ShopSecuritySearchForm;
import com.zy.pro.shop.model.Shop;
import com.zy.pro.shop.model.ShopUser;
import com.zy.pro.shop.service.ShopService;
import com.zy.pro.shop.service.ShopUserService;
import com.zy.pro.system.web.form.GlobalForm;
import com.zy.pro.system.web.form.MenuForm;
import com.zy.pro.user.model.User;
import com.zy.utils.MethodUtil;
import com.zy.utils.UploadHelper;
/**
 * 店铺管理控制器
 *
 */
@Controller
@RequestMapping("/control/shop/shopController")
public class ShopController extends BaseController {

	private static final Logger logger = Logger.getLogger(ShopController.class);
	
	/**
	 * 店铺管理服务类
	 */
	@Resource(name = "shopServiceImpl")
	private ShopService shopService;
	
	/**
	 * 店铺管理服务类
	 */
	@Resource(name = "shopUserServiceImpl")
	private ShopUserService shopUserService;
	
	/**
	 * 城市管理服务类
	 */
	@Resource(name = "cityServiceImpl")
	private CityService cityService;
	
	/**
	 * 保障金管理服务类
	 */
	@Resource(name = "securityServiceImpl")
	private SecurityService securityService;
	
	/**
	 * 店铺保障金管理服务类
	 */
	@Resource(name = "shopSecurityServiceImpl")
	private ShopSecurityService shopSecurityService;
	
	/**
	 * 卖家中心 展示店铺基本信息页面
	 * @param shop
	 * @param oldShopLogo
	 * @param map
	 * @param request
	 * @return
	 */
	@RequestMapping("/displayShop")
	public String displayShop(Shop shop,ModelMap map, HttpServletRequest request) {
		try {
			
			//设置菜单类型
			MenuForm menuForm = new MenuForm(MenuForm.MENU_TYPE_SELLER,MenuForm.PAR_TYPE_SELLER_INFO,MenuForm.SUB_TYPE_SELLER_INFO_INFO);
			map.addAttribute("menuForm", menuForm);
			
			shop = shopService.find(shop.getShopId());
			map.addAttribute("shop", shop);
			
			return "control/jsp/seller/information";
		} catch (Exception e) {
			logger.error(e);
			return "error";
		}
		
	}
	
	/**
	 * 卖家中心 展示编辑店铺基本信息页面
	 * @param shop
	 * @param oldShopLogo
	 * @param map
	 * @param request
	 * @return
	 */
	@RequestMapping("/displayShopEdit")
	public String displayShopEdit(Shop shop,ModelMap map, HttpServletRequest request) {
		try {

			//设置菜单类型
			MenuForm menuForm = new MenuForm(MenuForm.MENU_TYPE_SELLER,MenuForm.PAR_TYPE_SELLER_INFO,MenuForm.SUB_TYPE_SELLER_INFO_INFO);
			map.addAttribute("menuForm", menuForm);
			
			shop = shopService.find(shop.getShopId());
			map.addAttribute("shop", shop);
			return "control/jsp/seller/editInformation";
		} catch (Exception e) {
			logger.error(e);
			return "error";
		}
		
	}
	
	/**
	 * 卖家中心 编辑店铺基本信息
	 * @param shop
	 * @param oldShopLogo
	 * @param map
	 * @param request
	 * @return
	 */
	@RequestMapping("/edit")
	public String edit(Shop shop, String oldShopLogo,ModelMap map, HttpServletRequest request) {
		try {
			
			if(shop.getShopLogo() != null && shop.getShopLogo().indexOf("yibu") != -1){
				//将异步上传的Logo拷贝到正式路径下面
				String newPath = shop.getShopLogo().replaceAll("yibu", "shopLogo");
				UploadHelper.copyFile(shop.getShopLogo(), newPath, "yibu", request, logger);
				shop.setShopLogo(newPath);
				
				//删除旧Logo
				//UploadHelper.deletePic(oldShopLogo, "shopLogo", request, logger);
			}
			shopService.updateBySelective(shop);
			//map.addAttribute("shop", shop);
			return displayShop(shop,map,request);
		} catch (Exception e) {
			logger.error(e);
			return "error";
		}
		
	}
	
	/**
	 * 卖家中心 展示店铺保证金页面
	 * @param shop
	 * @param oldShopLogo
	 * @param map
	 * @param request
	 * @return
	 */
	@RequestMapping("/displayShopBond")
	public String displayShopBond(Shop shop,ModelMap map, HttpServletRequest request) {
		try {
			
			//设置菜单类型
			MenuForm menuForm = new MenuForm(MenuForm.MENU_TYPE_SELLER,MenuForm.PAR_TYPE_SELLER_INFO,MenuForm.SUB_TYPE_SELLER_INFO_BOND);
			map.addAttribute("menuForm", menuForm);
			
			
			shop = shopService.find(shop.getShopId());
			map.addAttribute("shop", shop);
			
			//判断如果店铺保证金是未缴状态,设置保证金
			BigDecimal shopBond = null;
			Security security = new Security();
			List<Security> sList = securityService.findAll(new SecuritySearchForm());
			if(!sList.isEmpty()){
				security = sList.get(0);
				shopBond = security.getSecurityMoney();
			}
			
			if(shop.getShopBondStatus() == 1 && security != null){
				shopBond = security.getSecurityMoney();
			}else if(shop.getShopBondStatus() == 2){
				//查看保障金记录
				ShopSecuritySearchForm form = new ShopSecuritySearchForm();
				form.setShopId(shop.getShopId());
				form.getOrderby().put("shopSecurityMoneyTime", "desc");
				PageView<ShopSecurity> pageView = new PageView<ShopSecurity>(12, form.getPage());
				form.setFirstResult(pageView.getFirstResult());
				form.setMaxResult(pageView.getMaxresult());
				pageView.setQueryResult(shopSecurityService.getData(form));
				map.addAttribute("pageView", pageView);
			}
			map.addAttribute("shopBond", shopBond);
			return "control/jsp/seller/bond";
		} catch (Exception e) {
			logger.error(e);
			return "error";
		}
		
	}
	
	/**
	 * 卖家中心 - 展示密码重置
	 * @param user
	 * @param map
	 * @return
	 */
	@RequestMapping("/displayEditShopUserCode")
	public String displayEditShopUserCode(ShopUser shopUser, ModelMap map) {
		try {
			//设置菜单类型
			MenuForm menuForm = new MenuForm(MenuForm.MENU_TYPE_SELLER,MenuForm.PAR_TYPE_SELLER_INFO,MenuForm.SUB_TYPE_SELLER_INFO_PASSWORD);
			map.addAttribute("menuForm", menuForm);
			
			shopUser = shopUserService.find(shopUser.getShopUserId());
			map.addAttribute("shopUser", shopUser);
			
			return "control/jsp/seller/seller_code";
		} catch(Exception e){
			logger.error(e);
			return "error";
		}
	}
	
	/**
	 *	验证是否重复
	 * @param form
	 */
	@RequestMapping("/checkOldPassword")
	@ResponseBody
	public JSON checkOldPassword(GlobalForm form, HttpServletRequest request){
		JSONObject json = new JSONObject();
		json.put("status", "n");
		json.put("info", "The old password is incorrect!");
		try {
			//param代表用户输入的原密码(没加密的)
			//JSON代表用户真实的原密码(加密后的)
			if(form.getParam() != null && !"".equals(form.getParam())){
				boolean falg = MethodUtil.ecompareMD5(form.getParam(), form.getJson());
				if(falg){
					json.put("status", "y");
					json.put("info", "Through verification!");
					logger.info("The user to reset the password - verify the original password, verified by！");
				}
				logger.info("The user to reset the password - verify the original password verification fails, the input error！");
			}
		} catch (Exception e) {
			logger.error("The user to reset the password - verify the original password verification fails, the input error！",e);
		}
		return json;
	}
	
	/**
	 * 用户中心 - 密码重置
	 * @param user
	 * @param map
	 * @return
	 */
	@RequestMapping("/editShopUserCode")
	public String editShopUserCode(ShopUser shopUser, ModelMap map) {
		try {
			//设置菜单类型
			MenuForm menuForm = new MenuForm(MenuForm.MENU_TYPE_SELLER,MenuForm.PAR_TYPE_SELLER_INFO,MenuForm.SUB_TYPE_SELLER_INFO_PASSWORD);
			map.addAttribute("menuForm", menuForm);
			
			shopUser.setPassword(MethodUtil.MD5(shopUser.getPassword()));
			shopUserService.updateBySelective(shopUser);
			map.addAttribute("shopUser", shopUser);
			map.addAttribute("editCodeStatus", "success");
			
			return "control/jsp/seller/seller_code";
		} catch(Exception e){
			logger.error(e);
			return "error";
		}
	}
}
